/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @PhotoAdapter.java  2014-9-15 上午11:56:53 - Carson
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.act.ActPhotos;
import com.android.tiango.baba.act.Actlbum;
import com.android.tiango.baba.chosephoto.ImageManager2;
import com.android.tiango.baba.util.PhotosSearch;
import com.android.tiango.baba.util.StringUtils;

public class PhotoAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<String> list = new ArrayList<String>();
	private boolean isDel = false;

	public PhotoAdapter(Context context, ArrayList<String> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		int size = list.size();
		if (size < Actlbum.MAX_CHOSE) {
			return list.size() + 1;
		} else {
			return list.size();
		}

	}

	public void resetAdapter(ArrayList<String> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (position == list.size()) {
			convertView = LayoutInflater.from(context).inflate(R.layout.item_addphoto, null);
			ImageView addPhoto = (ImageView) convertView.findViewById(R.id.iv_photo);
			addPhoto.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, ActPhotos.class);
					context.startActivity(intent);
				}
			});
		} else if (list.size() > position) {
			convertView = LayoutInflater.from(context).inflate(R.layout.item_photo, null);
			ImageView photo = (ImageView) convertView.findViewById(R.id.iv_photo);
			ImageView iv_del = (ImageView) convertView.findViewById(R.id.iv_del);
			String url = list.get(position);
			if (!StringUtils.isEmpty(url)) {
				ImageManager2.from(context)
						.displayImage(photo, url, R.drawable.img_loading_default);
			}
			iv_del.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (!isDel) {
						isDel = true;
						PhotosSearch.selectedDataList.remove(position);
						notifyDataSetChanged();
						isDel = false;
					}
				}
			});
		}

		return convertView;
	}

}
